<?php

namespace app\controller;

use app\model\Prize;
use support\Request;
use support\Response;

class PrizeController
{
    /**
     * 获取所有奖品列表（单活动模式）
     */
    public function list(Request $request): Response
    {
        $page = $request->get('page', 1);
        $pageSize = $request->get('pageSize', 20);
        // 直接获取所有奖品，不再需要activity_id参数
        $prizes = Prize::getList($page, $pageSize);
        return success($prizes);
    }

    /**
     * 创建奖品
     */
    public function save(Request $request): Response
    {
        $data = json_decode($request->rawBody(), true);

        if (!isset($data['id'])) {
            $prize = Prize::create($data);
        } else {
            $prize = Prize::find($data['id']);
            if (!$prize) {
                return json(['code' => 404, 'message' => '奖品不存在']);
            }
            $prize->save($data);
        }


        return success($prize);
    }

    /**
     * 删除奖品
     */
    public function delete(Request $request, int $id): Response
    {
        $prize = Prize::find($id);
        if (!$prize) {
            return json(['code' => 404, 'message' => '奖品不存在']);
        }

        $prize->delete();

        return json(['code' => 0, 'message' => '删除成功']);
    }
}
